package com.inspection.common;


import java.text.ParseException;

import com.inspection.job.HttpRequestJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

/**
 * Created by zhujun on 2015-6-5.
 *
 * Quartz调控类
 *
 */
public class QuartzManager {

    private static SchedulerFactory sf = new StdSchedulerFactory();
    private static String JOB_GROUPNAME = "JOB_GROUPNAME";
    private static String TRIGGER_GROUPNAME = "TRIGGER_GROUPNAME";

    /**
     *
     * 添加JOB
     *
     * @param jobName
     * @param clazz
     * @param time
     * @throws SchedulerException
     * @throws ParseException
     */
    public static void addJob(String jobName,Class clazz,String time)
            throws SchedulerException, ParseException{
        Scheduler sched = sf.getScheduler();
        JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(jobName, JOB_GROUPNAME).build(); //任务名，任务组，任务执行类
        CronTrigger trigger = (CronTrigger)TriggerBuilder.newTrigger().withIdentity(TRIGGER_GROUPNAME, JOB_GROUPNAME).withSchedule(CronScheduleBuilder.cronSchedule(time)).build();
        sched.scheduleJob(jobDetail,trigger);
        //启动
        if(!sched.isShutdown())
            sched.start();
    }


    /**
     * JobGroup,TriggerGroup进行分开
     * 区分不同的jobGroup,triggerGroup
     *
     * @param jobName
     * @param jobGroupName
     * @param triggerName
     * @param triggerGroupName
     * @param clazz
     * @param time
     * @param jobDataMap
     * @throws SchedulerException
     * @throws ParseException
     */
    public static void addJob(String jobName,String jobGroupName,
                              String triggerName,String triggerGroupName,
                              Class clazz,String time,JobDataMap jobDataMap)
            throws SchedulerException, ParseException{
        Scheduler sched = sf.getScheduler();
        JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(jobName, jobGroupName).usingJobData(jobDataMap).build();
        CronTrigger trigger = (CronTrigger)TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroupName).withSchedule(CronScheduleBuilder.cronSchedule(time)).build();
        sched.scheduleJob(jobDetail,trigger);
        if(!sched.isShutdown())
            sched.start();
    }

    public static void main(String[] args) throws ParseException, SchedulerException {
        JobDataMap jobDataMap = new JobDataMap();
        QuartzManager.addJob("33","s","33","s", HttpRequestJob.class,"0/10 * * * * ?",jobDataMap);
    }


}
